import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';

@Component({
  selector: 'app-stars',
  templateUrl: './stars.component.html',
  styleUrls: ['./stars.component.css']
})
export class StarsComponent implements OnInit {

  @Input()
  private rating: number;

  private starts: boolean[];

  /**
   * 属性输出
   */
  @Output('lastStart')
  private lastStart: EventEmitter<Starts> = new EventEmitter();


  @Output()
  private buy: EventEmitter<Starts> = new EventEmitter();

  constructor() {
    setInterval(() => {
      const starts: Starts = new Starts( Math.random() * 10);
      this.rating = starts.startCount;

      /**
       * 将lastStart发射出去，并订阅者使用
       */
      this.lastStart.emit(starts);
    }, 2000);
  }

  ngOnInit() {
    this.starts = [];
    for (let i = 1; i <= 5; i++) {
      this.starts.push(i > this.rating);
    }
    /*this.starts = [false, false, true, true, true];*/
  }

  buyItNow(event) {
    this.buy.emit(new Starts(this.rating));
  }

}

/**
 * 星级评价类
 */
export class Starts {
  constructor(public startCount: number) {

  }
}
